﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;

namespace TST.SharePointObjects
{
    internal class ObjectReferenceList
    {
        //public ObjectReferenceList(LicensingMode licensingMode)
        //     :base(licensingMode)
        //{
        //}
        private List<ObjectReference> _references;
        private LicensingMode _licensingMode;

        protected LicensingMode LicensingMode
        {
            get { return _licensingMode; }
        }

        internal ObjectReferenceList(LicensingMode licensingMode)
        {
            _references = new List<ObjectReference>();
            _licensingMode = licensingMode;
        }

        //protected virtual void doLoadFromWeb(SPWeb web)
        //{
        //}

        //protected virtual void doLoadFromWeb(SPWeb web, string groupFilter, string nameSearch)
        //{
        //}

        //public void LoadFromWeb(SPWeb web)
        //{
        //    if (web == null)
        //        return;
        //    Clear();
        //    doLoadFromWeb(web);
        //}

        //public void LoadFromWeb(SPWeb web, string groupFilter, string nameSearch)
        //{
        //    if (web == null)
        //        return;
        //    Clear();
        //    doLoadFromWeb(web, groupFilter, nameSearch);
        //}

        public void Clear()
        {
            _references.Clear();
        }

        public void Add(ObjectReference ObjectReference)
        {
            _references.Add(ObjectReference);
        }

        public List<ObjectReference> Items
        {
            get
            {
                return _references;
            }
        }

        public ObjectReference GetItemById(string id)
        {
            foreach (ObjectReference obj in _references)
            {
                if (string.Compare(obj.Id, id, true) == 0)
                {
                    return obj;
                }
            }
            return null;
        }

        public virtual ObjectReferenceType ObjectReferenceType
        {
            get { return ObjectReferenceType.Unknown; }
        }

        public void Sort(string expression, SortDirection direction)
        {
            switch (expression.ToLower())
            {
                case "name":
                    switch (direction)
                    {
                        case SortDirection.Ascending:
                            _references.Sort(ObjectReferenceComparer.CompareByName);
                            break;
                        case SortDirection.Descending:
                            _references.Sort(ObjectReferenceComparer.CompareByNameDesc);
                            break;
                    }
                    break;
                case "referencetypedescription":
                    switch (direction)
                    {
                        case SortDirection.Ascending:
                            _references.Sort(ObjectReferenceComparer.CompareByType);
                            break;
                        case SortDirection.Descending:
                            _references.Sort(ObjectReferenceComparer.CompareByTypeDesc);
                            break;
                    }
                    break;
                case "site":
                    switch (direction)
                    {
                        case SortDirection.Ascending:
                            _references.Sort(ObjectReferenceComparer.CompareBySite);
                            break;
                        case SortDirection.Descending:
                            _references.Sort(ObjectReferenceComparer.CompareBySiteDesc);
                            break;
                    }
                    break;
                case "list":
                    switch (direction)
                    {
                        case SortDirection.Ascending:
                            _references.Sort(ObjectReferenceComparer.CompareBylist);
                            break;
                        case SortDirection.Descending:
                            _references.Sort(ObjectReferenceComparer.CompareBylistDesc);
                            break;
                    }
                    break;
            }
        }

    }
}